package com.tianhui.consignortwofei.mvp.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.entity.PictureInfo;
import com.fgs.common.entity.UploadPictureInfo;
import com.fgs.common.interf.OnLocationCheckListener;
import com.fgs.common.pictureSelector.SelectPicture;
import com.fgs.common.uploadPicture.IUploadPicture;
import com.fgs.common.uploadPicture.UploadPicture;
import com.fgs.common.utils.CommonUtil;
import com.fgs.common.utils.ImageUtil;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.utils.WaterMarkerUtils;
import com.luck.picture.lib.entity.LocalMedia;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.constant.PictureType;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fgs
 * @date 2020/4/2
 * @description 确认装车
 **/
public abstract class OrderUploadPictureActivity extends BaseActivity {


    protected OrderModel mOrderModel;
    protected OrderInfo mOrderInfo;
    protected String mOrderId;
    private SelectPicture mSelectPicture;
    private SelectPicture mVehicleImageSelectPicture;
    //装车单据照片
    List<String> mSelectPictureList = new ArrayList<>();
    //装车车辆照片
    List<String> mVehicleImageSelectPictureList = new ArrayList<>();


    @Override
    public void init() {

        mOrderModel = new OrderModel();
        mSelectPicture = new SelectPicture();
        mSelectPicture.setCrop(true);
        mVehicleImageSelectPicture = new SelectPicture();
        mVehicleImageSelectPicture.setCrop(true);

        mSelectPicture.setOnLocationCheckListener(new OnLocationCheckListener() {
            @Override
            public boolean onLocationCheck() {
//                return isHasLocationInfo();
                return true;
            }
        });

        mSelectPicture.selectMultiPicture(this, new SelectPicture.OnSelectResultCallback() {
            @Override
            public void selectResultCallback(List<LocalMedia> selectList) {

                if (null != selectList && selectList.size() > 0) {

                    mSelectPictureList.clear();
                    int size = selectList.size();
                    String path = "";
                    for (int i = 0; i < size; i++) {
                        path = selectList.get(i).getCompressPath();
                        requestPictureUpload(PictureType.TYPE_INVOICES_IMAGE, path, i);
                    }
                }
            }

            @Override
            public void selectResultPath(String path) {

            }

            @Override
            public void deletePictureCallback(List<LocalMedia> selectList, int position) {
                int size = mSelectPictureList.size();
                LogUtil.print("position--" + position + "\n" + "size--" + size);
                if (position < size) {
                    mSelectPictureList.remove(position);
                }

            }
        }, getInvoicesImageRecyclerView());


//        mVehicleImageSelectPicture.setOnLocationCheckListener(new OnLocationCheckListener() {
//            @Override
//            public boolean onLocationCheck() {
//                return isHasLocationInfo();
//            }
//        });

        mVehicleImageSelectPicture.selectMultiPicture(this, new SelectPicture.OnSelectResultCallback() {
            @Override
            public void selectResultCallback(List<LocalMedia> selectList) {

                if (null != selectList && selectList.size() > 0) {

                    mVehicleImageSelectPictureList.clear();
                    int size = selectList.size();
                    String path = "";
                    for (int i = 0; i < size; i++) {
                        path = selectList.get(i).getCompressPath();
                        requestPictureUpload(PictureType.TYPE_VEHICLE_IMAGE, path, i);
                    }
                }
            }

            @Override
            public void selectResultPath(String path) {

            }

            @Override
            public void deletePictureCallback(List<LocalMedia> selectList, int position) {
                int size = mVehicleImageSelectPictureList.size();
                LogUtil.print("position--" + position + "\n" + "size--" + size);
                if (position < size) {
                    mVehicleImageSelectPictureList.remove(position);
                }

            }
        }, getVehicleImageRecyclerView());


        Bundle bundle = getIntent().getExtras();
        if (null != bundle && !bundle.isEmpty()) {
            mOrderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
            if (null != mOrderInfo) {
                mOrderId = mOrderInfo.dispatchno;
            }
        }

    }


    /**
     * 装车车辆照片
     *
     * @return
     */
    public abstract RecyclerView getVehicleImageRecyclerView();

    /**
     * 装车单据照片
     *
     * @return
     */
    public abstract RecyclerView getInvoicesImageRecyclerView();

    public String getInvoicesImageId() {

        return CommonUtil.getSelectPictureId(mSelectPictureList);
    }

    public String getVehicleImageId() {
        return CommonUtil.getSelectPictureId(mVehicleImageSelectPictureList);
    }

    private void requestPictureUpload(int type, String path, int position) {
        if (TextUtils.isEmpty(path)) return;
        //添加水印
        Bitmap bitmap = ImageUtil.getBitmap(path);
        //水印内容
//        String address = mAddress;
//        if (TextUtils.isEmpty(address)) {
//            address = SharedPreferenceManager.getInstance().getAddress();
//        }

        //添加水印后的图片
        Bitmap resultBitmap;

        String address = getAddress();
//        String date = DateUtil.getStandardTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
        StringBuilder stringBuilder = new StringBuilder();

        if (!TextUtils.isEmpty(address)) {
            String date = getTime();
            stringBuilder.append(address);
            stringBuilder.append("\n");
            stringBuilder.append(date);

            resultBitmap = WaterMarkerUtils.drawTextToLeftBottom(bitmap,
                    stringBuilder.toString(),
                    getResources().getColor(R.color.colorRedDark));
        } else {
            resultBitmap = bitmap;
        }

        //上传图片
        uploadPicture(type, com.fgs.common.utils.ImageUtil.getFile(resultBitmap), position);
//        //压缩图片
//        CompressController.getInstance().pictureCompress(resultBitmap, new OnPictureCompressListener() {
//            @Override
//            public void onSuccess(File file) {
//                //上传图片
//                uploadPicture(type, file, position);
//            }
//
//            @Override
//            public void onError(Throwable throwable) {
//
//            }
//        });

    }

    public abstract String getAddress();

    public abstract String getTime();

    private void uploadPicture(int type, File file, int position) {
        UploadPictureInfo uploadPictureInfo = new UploadPictureInfo();
        uploadPictureInfo.file = file;
        uploadPictureInfo.pictureModule = "dispatchSource";
        new UploadPicture(new IUploadPicture.UploadPictureCallback() {
            @Override
            public void onSuccess(UploadPictureInfo uploadPictureInfo, PictureInfo pictureInfo) {
                processPictureUploadResult(pictureInfo, type);
            }

            @Override
            public void onFailed(UploadPictureInfo uploadPictureInfo) {
                removePicture(type, position);
            }
        }).uploadPicture(this, uploadPictureInfo);

    }

    public void clearSelectPicture() {
        mSelectPicture.clearPicture();
        mVehicleImageSelectPicture.clearPicture();
        mSelectPictureList.clear();
        mVehicleImageSelectPictureList.clear();
    }

    private void removePicture(int type, int position) {
        if (type == PictureType.TYPE_INVOICES_IMAGE) {
            mSelectPicture.removePicture(position);
        } else if (type == PictureType.TYPE_VEHICLE_IMAGE) {
            mVehicleImageSelectPicture.removePicture(position);
        }
    }

    private void processPictureUploadResult(PictureInfo pictureInfo, int type) {
        switch (type) {
            case PictureType.TYPE_INVOICES_IMAGE:
                mSelectPictureList.add(pictureInfo.id);
                break;
            case PictureType.TYPE_VEHICLE_IMAGE:
                mVehicleImageSelectPictureList.add(pictureInfo.id);
                break;
        }
        LogUtil.print("pictureID---" + pictureInfo.id);
    }


}
